<?php

namespace app\crontab\task;

use app\common\library\task\ImportService;
use fast\Http;
use think\Db;


/**
 * 计划的定时任务 
 */ 
class Dbexport extends Common
{
    const COPY_CONNECT = [
        // 数据库类型
        'type'        => 'mysql',
        // 数据库连接DSN配置
        'dsn'         => '',
        // 服务器地址
        'hostname'    => '39.106.59.93',
        // 数据库名
        'database'    => 'admin',
        // 数据库用户名
        'username'    => 'admin',
        // 数据库密码
        'password'    => 'y18615398935',
        // 数据库连接端口
        'hostport'    => '',
        // 数据库连接参数
        'params'      => [],
        // 数据库编码默认采用utf8
        'charset'     => 'utf8',
        // 数据库表前缀
        'prefix'      => 'fa_',
    ];
    protected $copy;
    public function __construct(array $params = [])
    {
        parent::__construct($params);
        $this->copy = Db::connect(self::COPY_CONNECT);
    }

    /*导入公司*/
    public function importCompany() {
        $list = $this->copy->name('zb_company')->select();
        foreach ($list as $k => $v) {
            $data['row']['name'] = $v['name'];
            $data['row']['id'] = $v['id'];
            $data['row']['code'] = $v['code'];
            $data['row']['concat_phone'] = $v['concat_phone'];
            $data['row']['province'] = $v['province'];
            $data['row']['city'] = $v['city'];
            $data['row']['area'] = $v['area'];
            $data['row']['category'] = $v['category'];
            $data['row']['register_money'] = $v['register_money'];
            $data['row']['employment'] = $v['employment'];
            $data['row']['register_time'] = $v['register_time'];
            $data['row']['register_place'] = $v['register_place'];
            $data['row']['registry_office'] = $v['registry_office'];
            $data['row']['credit_code'] = $v['credit_code'];
            $data['row']['organization_code'] = $v['organization_code'];
            $data['row']['business_scope'] = $v['business_scope'];
            $data['row']['detail'] = $v['detail'];
            $url = 'http://www.hzhong.com/admin/company/add';
            Http::post($url, $data);
        }
    }

    /*导入任务 <=1085*/
    public function importTask() {
        $category = [
            35 => 10,
            49 => 9,
            48 => 8,
            23 => 7,
            36 => 6,
            50 => 5,
            30 => 4,
            51 => 3,
            53 => 2,
            55 => 1,
            56 => 13,
            57 => 14,
        ];
        $list = $this->copy->name('zb_task')->where(['id'=> ['gt', 1096]])->chunk(1000, function($items) use ($category) {
            foreach ($items as $v) {
                $data['name'] = $v['name'];
                $data['id'] = $v['id'];
                $data['image'] = $v['image'];
                $data['introduce'] = $v['introduce'];
                $data['zbcategory_id'] = $category[$v['category_id']];
                $data['company_id'] = $v['company_id'];
                $data['createtime'] = $v['createtime'];
                $data['province'] = $v['province'];
                $data['city'] = $v['city'];
                $data['area'] = $v['area'];
                $data['content'] = $v['content'];
                $data['shutdownswitch'] = $v['shutdownswitch'];
                $data['status'] = $v['status'];
                $data['remark'] = $v['remark'];
                $data['type'] = $v['type'];

                $zbtask = Db::name('zbtask');
                if (Db::name('zbtask')->where(['id' => $v['id']])->count() > 0) {
                    continue;
                }
                try {
                    $zbtask->insert($data);
                } catch (\Exception $e) {
                    echo $e->getMessage();
                }
            }
        });;
    }

    /*导入众包费*/
    public function importMoney() {
        $where['type'] = ['neq', 2];
        $where['status'] = 1;
       // $where['id'] = ['lt', 526];
        $list = $this->copy->name('zb_task_money')->where($where)->chunk(1000, function($items) {
            foreach ($items as $v) {
                try {
                    Db::startTrans();
                    $user = $this->copy->name('user')->where(['id'=> $v['user_id']])->find();
                    $bank = $this->copy->name('zb_bank')->where(['user_id' => $v['user_id']])->find();
                    /*$curUser = Db::name('user')->where(['identity_card' => $user['identity_card']])->find();
                    if ($curUser) {
                        if (Db::name('zbtask_money')->where(['user_id' => $curUser['id'], 'task_id'=> $v['task_id']])->count() > 0) {
                            doLog('\n continue:' . $v['id'], 'copyimportmoney');
                            continue;
                        }
                    }*/
                    $data['task_id'] = $v['task_id'];
                    $data['real_name'] = $user['real_name'];
                    $data['id'] = $v['id'];
                    $data['identity_card'] = $user['identity_card'];
                    $data['bank_card'] = $bank['bank_card'];
                    $data['mobile'] = $bank['mobile'];
                    $data['money'] = $v['get_money'];
                    $data['type'] = 1;
                    $data['money_type'] = 2;
                    $data['item'] = $v['item'];
                    $data['status'] = 1;
                    $data['is_import'] = 0;
                    $data['type'] = $v['type'];
                    $data['transfer_type'] = $v['transfer_type'];
                    $data['order_sn'] = $v['order_sn'];
                    $data['remark'] = $v['remark'];
                    $data['pay_time'] = $v['pay_time'];
                    $data['is_online'] = $v['pay_type'] == 0 ? 0 : 1;

                    ImportService::instance()->setData($data)->createUser()->createOrder()->insertData();
                    Db::commit();
                } catch (\Exception $e) {
                    doLog('copyimportmoney:' . $v['id'] . $e->getMessage(), 'copyimportmoney');
                    Db::rollback();
                }

            }
        }, 'id');
    }

    public function moneyLog() {

    }
}